import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'page1.dart';
import 'page2.dart';
import 'page3.dart';

class TestBottomNavigatorPage extends StatefulWidget {
  @override
  _TestBottomNavigatorPageState createState() => _TestBottomNavigatorPageState();
}

class _TestBottomNavigatorPageState extends State<TestBottomNavigatorPage> {
  int _selectedIndex = 0;
  List<Widget> _pages = [Page1(), Page2(), Page3()];
  var _pageController = PageController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView.builder(
          controller: _pageController,
          physics: NeverScrollableScrollPhysics(),
          onPageChanged: _pageChanged,
          itemCount: _pages.length,
          itemBuilder: (context, index) => _pages[index]),
      bottomNavigationBar: BottomNavigationBar(
        // 底部导航
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/tabIcon_unselect1.png',
                height: 70.w,
                width: 70.w,
              ),
              title: Text('Home'),
              activeIcon: Image.asset(
                'assets/images/tabIcon_select1.png',
                height: 70.w,
                width: 70.w,
              )),
          BottomNavigationBarItem(
            icon: Image.asset(
              'assets/images/tabIcon_unselect2.png',
              height: 70.w,
              width: 70.w,
            ),
            activeIcon: Image.asset(
              'assets/images/tabIcon_select2.png',
              height: 70.w,
              width: 70.w,
            ),
            title: Text('Home'),

          ),
          BottomNavigationBarItem(
              icon: Image.asset(
                'assets/images/tabIcon_unselect3.png',
                height: 70.w,
                width: 70.w,
              ),
              title: Text('Home'),
              activeIcon: Image.asset(
                'assets/images/tabIcon_select3.png',
                height: 70.w,
                width: 70.w,
              )),
        ],
        currentIndex: _selectedIndex,
        fixedColor: Colors.blue,
        onTap: _onTabTapped,
      ),
      floatingActionButton: FloatingActionButton(
          //悬浮按钮
          child: Icon(Icons.add),
          onPressed: _onAdd),
    );
  }

  void _onAdd() {}

  void _pageChanged(int index) {
    setState(() {
      if (_selectedIndex != index) _selectedIndex = index;
    });
  }

  void _onTabTapped(int index) {
    if (_selectedIndex != index) {
      _pageController.jumpToPage(index);
//      _pageController.animateToPage(index, curve: Curves.ease, duration: Duration(milliseconds: 2000));
    }
  }

  @override
  void dispose() {
    super.dispose();
    _pageController.dispose();
  }
}
